<?php
include('Publication.php');
include('LinRegression.php');


if(isset($_GET['domainID'])) {
    $domainID = $_GET['domainID'];
    $firstSubDomainID = $_GET['firstSubDomainID'];
    $secondSubDomainID = $_GET['secondSubDomainID'];
    $yearStart = intval($_GET['yearStart']);
    $yearEnd = intval($_GET['yearEnd']);

ini_set('max_execution_time', 300);


$results = array();
$results2 = array();

for ($i=$yearStart; $i<=$yearEnd; $i++)
{
    $pub = new Publication;

    $pub2 = new Publication;


    
   
    $pub->domainID = $domainID;

    
    $pub2->domainID = $domainID;
   
    
   
    $pub->subDomainID = $firstSubDomainID;
    $pub2->subDomainID = $secondSubDomainID;
    
     
     
    $pub->yearStart=$i;
    $pub->yearEnd=$i;
    
    $pub2->yearStart=$i;
    $pub2->yearEnd=$i;
    
 
        
    $pub->makeQuery();
  

    $pub2->makeQuery();
  
    
    //echo $pub->getPublicationCount();
    
    $results[$i] = $pub->getPublicationCount();
    $results2[$i] = $pub2->getPublicationCount();

}

$yearArray = array();
$pubArray = array();
$sum = 0;

foreach ($results  as $key => $value) 
{
    
    $val = intval($value);
    array_push($yearArray,$key);
    array_push($pubArray,$val + $sum);
    $results[$key] = $val + $sum;
    
    $sum +=$val;
}

$pubArray2 = array();
$sum = 0;
foreach ($results2  as $key => $value) 
{
    
    $val = intval($value);
   
    array_push($pubArray2,$val + $sum);
    $results2[$key] = $val + $sum;
    
    $sum +=$val;
}

$regObj = array();
$regObj = LinRegression::linear_regression($yearArray,$pubArray);

$linearArray = array();
$yValue;
$m = floatval($regObj["m"]);
$b = floatval($regObj["b"]);

for ($i=$yearStart; $i<=$yearEnd+10; $i++) 
{
    $yValue = $m * $i + $b;
    array_push($linearArray,$yValue);
}

$regObj = array();
$regObj = LinRegression::linear_regression($yearArray,$pubArray2);

$linearArray2 = array();
$yValue;
$m2 = floatval($regObj["m"]);
$b2 = floatval($regObj["b"]);

for ($i=$yearStart; $i<=$yearEnd+10; $i++) 
{
    $yValue = $m2 * $i + $b2;
    array_push($linearArray2,$yValue);
}



$rog =  LinRegression::meanRateOfGrowth($pubArray);
$rog2 = LinRegression::meanRateOfGrowth($pubArray2);

$jsonObj = new jsonObj;
$jsonObj->first = $results;
$jsonObj->second = $results2;
$jsonObj->linear = $linearArray;
$jsonObj->linear2 = $linearArray2;
$jsonObj->slope = $m;
$jsonObj->slope2 = $m2;
$jsonObj->rog = $rog;
$jsonObj->rog2 = $rog2;

echo json_encode($jsonObj);
}

class jsonObj 
{
    public $first, $second, $linear, $linear2, $slope, $slope2, $rog, $rog2;
}

?>